package com.mf.ergate.controller.auth;

import com.mf.ergate.api.auth.RoleInfoRemoteService;
import com.mf.ergate.api.auth.model.dto.RoleInfoCreateDto;
import com.mf.ergate.api.auth.model.dto.RoleInfoDeleteDto;
import com.mf.ergate.api.auth.model.dto.RoleInfoQueryDto;
import com.mf.ergate.api.auth.model.dto.RoleInfoUpdateDto;
import com.mf.ergate.api.auth.model.vo.RoleInfoVo;
import com.mf.ergate.common.utils.MfBeanUtils;
import com.mf.ergate.domain.model.auth.RoleInfoEntity;
import com.mf.ergate.service.auth.RoleInfoService;
import com.mf.framework.common.model.Page;
import com.mf.framework.common.model.RemotePage;
import com.mf.framework.common.model.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色信息Controller
 * User: zhaoming
 * DateTime: 2019-02-08
 * To change this template use File | Settings | File Templates.
 */
@RestController
public class RoleInfoController implements RoleInfoRemoteService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private RoleInfoService roleInfoService;

    /**
     * 新增
     * @param roleInfoCreateDto 角色信息新增Dto
     * @return 角色信息
     */
    public Result<RoleInfoVo> insert(@RequestBody RoleInfoCreateDto roleInfoCreateDto) {
        RoleInfoEntity roleInfoEntity = MfBeanUtils.copyObject(roleInfoCreateDto, RoleInfoEntity.class);
        roleInfoEntity = roleInfoService.insert(roleInfoEntity);
        RoleInfoVo roleInfoVo = MfBeanUtils.copyObject(roleInfoEntity, RoleInfoVo.class);
        return new Result<>(roleInfoVo);
    }

    /**
     * 根据唯一编号更新
     * @param roleInfoUpdateDto 角色信息更新Dto
     * @return 执行结果
     */
    public Result<Boolean> updateByUniqueNo(@RequestBody RoleInfoUpdateDto roleInfoUpdateDto) {
        RoleInfoEntity roleInfoEntity = MfBeanUtils.copyObject(roleInfoUpdateDto, RoleInfoEntity.class);
        Boolean flag = roleInfoService.updateByUniqueNo(roleInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号删除(逻辑删除)
     * @param roleInfoDeleteDto 角色信息删除Dto
     * @return 结果集
     */
    public Result<Boolean> deleteByUniqueNo(@RequestBody RoleInfoDeleteDto roleInfoDeleteDto){
        RoleInfoEntity roleInfoEntity = MfBeanUtils.copyObject(roleInfoDeleteDto, RoleInfoEntity.class);
        Boolean flag = roleInfoService.deleteByUniqueNo(roleInfoEntity);
        return new Result<>(flag);
    }

    /**
     * 根据唯一编号查询
     * @param uniqueNo 唯一编号
     * @return 角色信息
     */
    public Result<RoleInfoVo> selectByUniqueNo(@RequestBody String uniqueNo) {
        RoleInfoEntity roleInfoEntity = roleInfoService.selectByUniqueNo(uniqueNo);
        RoleInfoVo roleInfoVo = MfBeanUtils.copyObject(roleInfoEntity, RoleInfoVo.class);
        return new Result<>(roleInfoVo);
    }

    /**
     * 根据条件查询
     * @param roleInfoQueryDto 角色信息查询Dto
     * @return 角色信息列表
     */
    public Result<List<RoleInfoVo>> selectByCondition(@RequestBody RoleInfoQueryDto roleInfoQueryDto) {
        List<RoleInfoEntity> roleInfoEntities = roleInfoService.selectByCondition(roleInfoQueryDto);
        List<RoleInfoVo> roleInfoVos = MfBeanUtils.copyList(roleInfoEntities, RoleInfoVo.class);
        return new Result<>(roleInfoVos);
    }

    /**
     * 根据条件查询数量
     * @param roleInfoQueryDto 角色信息查询Dto
     * @return 数量
     */
    public Result<Integer> selectCount(@RequestBody RoleInfoQueryDto roleInfoQueryDto) {
        return new Result<>(roleInfoService.selectCount(roleInfoQueryDto));
    }

    /**
     * 根据条件分页查询
     * @param remotePage 远程分页对象
     * @return 角色信息分页列表
     */
    public Result<Page<RoleInfoVo>> selectPage(@RequestBody RemotePage<RoleInfoQueryDto> remotePage){
        Page<RoleInfoEntity> roleInfoEntityPage = roleInfoService.selectPage(remotePage.getPage(), remotePage.getObj());
        Page<RoleInfoVo> page = MfBeanUtils.copyPage(roleInfoEntityPage, RoleInfoVo.class);
        return new Result<>(page);
    }

    /**
     * 删除角色及相关信息(逻辑删除)
     * @param roleInfoDeleteDto 角色信息删除Dto
     * @return 执行结果
     */
    public Result<Boolean> deleteCompleteByUniqueNo(@RequestBody RoleInfoDeleteDto roleInfoDeleteDto){
        RoleInfoEntity roleInfoEntity = MfBeanUtils.copyObject(roleInfoDeleteDto, RoleInfoEntity.class);
        Boolean flag = roleInfoService.deleteCompleteByUniqueNo(roleInfoEntity);
        return new Result<>(flag);
    }

}